package com.hubu.controller;

import com.alibaba.fastjson.JSONObject;
import com.hubu.annotation.PassToken;
import com.hubu.annotation.UserLoginToken;
import com.hubu.domain.User;
import com.hubu.result.R;
import com.hubu.service.UserService;
import com.hubu.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;

/**
 * @description:
 * @author: hubu
 * @time: 2023/4/12 19:45
 */
@RestController
public class UserController {
    @Autowired
    UserService userService;

    @PassToken
    @PostMapping("/login")
    public R login(@RequestBody User user) throws IOException {
        System.out.println(user);
        User userforbase =  userService.getUserByName(user.getUsername());
        if(userforbase==null){
            return R.error().message("登录失败，用户不存在");
        }
        if(!userforbase.getPassword().equals(user.getPassword())){
            return R.error().message("登录失败，密码错误");
        }
        String token = TokenUtil.getToken(userforbase);
        userService.saveToken(userforbase.getId(),token);
        return R.ok().data("tokenName",token);
    }

    @UserLoginToken
    @RequestMapping("/getmessage")
    public R getmessage(){
        return R.ok().message("你已经通过验证");
    }

    @PostMapping("/userInfo")
    public R getUserInfo(@RequestBody String json){
        JSONObject object= JSONObject.parseObject(json);
        String token = object.getString("accessToken");
        User user = userService.getUser(token);
        System.out.println(user);
        HashMap<String, Object> map = new HashMap<>();
        map.put("username",user.getUsername());
        map.put("avatar",user.getAvatar());
        return R.ok().data(map);

    }
    @PostMapping("/register")
    public R register(@RequestBody User user){
        /*JSONObject object= JSONObject.parseObject(params);
        String userStr = object.getString("param");
        User user = JSONObject.toJavaObject(JSONObject.parseObject(userStr), User.class);
        System.out.println(user);*/
        // 校验用户输入数据的合法性
        if (user.getUsername() == null || user.getUsername().isEmpty() ||
                user.getPassword() == null || user.getPassword().isEmpty() ||
            user.getPhone() == null || user.getPhone().isEmpty()) {
            return R.error().message("用户名、密码和手机号不能为空");
        }
        // 检查用户名是否已经存在
        Integer flag = userService.checkUsername(user.getUsername());
        if (flag > 0){
            return R.error().message("用户名已存在");
        }
        userService.register(user);
        return R.ok().message("注册成功！！！");


    }
    @PostMapping("/logout")
    public R logout(){
        return R.ok().message("success");
    }

    @GetMapping("/isExist/{username}")
    public R checkUsername(@PathVariable String username){
        Integer num = userService.checkUsername(username);
        return R.ok().data("exist", num > 0 ? 1 : 0);
    }
}
